using System.Net.Sockets;

namespace Admin.NET.Application.GuangHeMeter;

/// <summary>
/// 用户对象
/// 注意事项:一个Socket的Send和Receive最好分别对应一个SocketAsyncEventArgs
/// </summary>
public class AsyncUserToken
{
	/// <summary>
	/// 接收数据的SocketAsyncEventArgs
	/// </summary>
	private SocketAsyncEventArgs _receiveEventArgs;

	/// <summary>
	/// 发送数据的SocketAsyncEventArgs
	/// </summary>
	private SocketAsyncEventArgs _sendEventArgs;

	/// <summary>
	/// 接收数据的缓冲区
	/// </summary>
	private byte[] _asyncReceiveBuffer;

	/// <summary>
	/// 发送数据的缓冲区
	/// </summary>
	private byte[] _asyncSendBuffer;

	/// <summary>
	/// 动态的接收缓冲区
	/// </summary>
	private DynamicBufferManager _receiveBuffer;

	/// <summary>
	/// 动态的发送缓冲区
	/// </summary>
	private DynamicBufferManager _sendBuffer;

	/// <summary>
	/// 连接的Socket对象
	/// </summary>
	private Socket _connectSocket;

	/// <summary>
	/// 判断资源是否被释放
	/// </summary>
	private bool _isDispose;

	/// <summary>
	/// 判断资源是否被回收
	/// </summary>
	public bool IsDispose
	{
		get
		{
			return _isDispose;
		}
		set
		{
			_isDispose = value;
		}
	}

	public SocketAsyncEventArgs ReceiveEventArgs
	{
		get
		{
			return _receiveEventArgs;
		}
		set
		{
			_receiveEventArgs = value;
		}
	}

	public SocketAsyncEventArgs SendEventArgs
	{
		get
		{
			return _sendEventArgs;
		}
		set
		{
			_sendEventArgs = value;
		}
	}

	public Socket ConnectSocket
	{
		get
		{
			return _connectSocket;
		}
		set
		{
			_connectSocket = value;
		}
	}

	public DynamicBufferManager ReceiveBuffer
	{
		get
		{
			return _receiveBuffer;
		}
		set
		{
			_receiveBuffer = value;
		}
	}

	public DynamicBufferManager SendBuffer
	{
		get
		{
			return _sendBuffer;
		}
		set
		{
			_sendBuffer = value;
		}
	}

	public void CloseDispose()
	{
		_connectSocket.Dispose();
		_isDispose = true;
	}

	public void InitDisPose()
	{
		_isDispose = false;
	}

	public AsyncUserToken(int ReceiveBufferSize)
	{
		_isDispose = false;
		_connectSocket = null;
		_receiveEventArgs = new SocketAsyncEventArgs();
		_receiveEventArgs.UserToken = this;
		_asyncReceiveBuffer = new byte[ReceiveBufferSize];
		_receiveEventArgs.SetBuffer(_asyncReceiveBuffer, 0, _asyncReceiveBuffer.Length);
		_sendEventArgs = new SocketAsyncEventArgs();
		_sendEventArgs.UserToken = this;
		_asyncSendBuffer = new byte[ReceiveBufferSize];
		_sendEventArgs.SetBuffer(_asyncSendBuffer, 0, _asyncSendBuffer.Length);
		_receiveBuffer = new DynamicBufferManager(4096);
		_sendBuffer = new DynamicBufferManager(4096);
	}
}
